//
//  ManHuaMainViewController.m
//  Double
//
//  Created by 孟令博 on 15/5/25.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "ManHuaMainViewController.h"
#import "ManhuaViewController.h"
#import "ManHuaClassifyViewController.h"

@interface ManHuaMainViewController ()<UIScrollViewDelegate>
@property (nonatomic, retain) UISegmentedControl *segment;
@property (nonatomic, retain) UIScrollView *scroll;
@end

@implementation ManHuaMainViewController

- (void)dealloc
{
    [_segment release];
    [_scroll release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)loadView
{
    [super loadView];
    [self createScrollView];
    [self createSegment];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.titleView = _segment;
}

#pragma mark -
#pragma mark 创建scrollView
- (void)createScrollView
{
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.scroll.contentSize = CGSizeMake(self.view.frame.size.width * 2, 0);
    self.scroll.pagingEnabled = YES;
    self.scroll.delegate = self;
    //片单页面偏移量
    ManhuaViewController *subject = [[ManhuaViewController alloc] init];
    subject.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self addChildViewController:subject];
    
    //分类页面偏移量
    ManHuaClassifyViewController *recommend = [[ManHuaClassifyViewController alloc] init];
    recommend.view.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self addChildViewController:recommend];
    [self.scroll addSubview:subject.view];
    [self.scroll addSubview:recommend.view];
    
    [self.view addSubview:self.scroll];
    [subject release];
    [recommend release];
    [self.scroll release];
}

#pragma mark -
#pragma mark 创建segmentControl
- (void)createSegment
{
    NSArray *array = [NSArray arrayWithObjects:@"推荐",@"片单", nil];
    self.segment = [[UISegmentedControl alloc] initWithItems:array];
    self.segment.frame = CGRectMake(self.view.frame.size.width / 4, 0, self.view.frame.size.width / 2, 30);
    self.segment.selectedSegmentIndex = 0;
    self.segment.layer.cornerRadius = 20;
    self.segment.tintColor = [UIColor orangeColor];
    [self.segment addTarget:self action:@selector(segmentChanged1:) forControlEvents:UIControlEventValueChanged];
    
    [self.segment release];
}

#pragma mark -
#pragma mark scrollView协议
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x == 0) {
        self.segment.selectedSegmentIndex = 0;
    } else if (scrollView.contentOffset.x == self.scroll.frame.size.width){
        self.segment.selectedSegmentIndex = 1;
    }

}



#pragma mark -
#pragma mark segmentControl 点击事件
- (void)segmentChanged1:(UISegmentedControl *)seg
{
    switch (seg.selectedSegmentIndex) {
        case 0:
            self.scroll.contentOffset = CGPointMake(0, 0);
            break;
        case 1:
            self.scroll.contentOffset = CGPointMake(self.scroll.frame.size.width, 0);
            break;
        
        default:
            break;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
